package cn.itsource.product.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.mapper.ProductDetailMapper;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product>implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    @Transactional
    public void save(Product product) {
            if(product.getId()==null){//添加
                //保存产品服务表
                productMapper.save(product);
                ProductDetail productDetail = product.getProductDetail();
                //给服务明细的设置product设置属性
                productDetail.setProduct(product);
                ///保存服务明细表
                productDetailMapper.save(productDetail);
            }else{//修改
                //修改产品服务表
                productMapper.update(product);
                //获取详情
                ProductDetail productDetail = product.getProductDetail();
                //设置产品
                productDetail.setProduct(product);
                //修改产品详情
                productDetailMapper.update(productDetail);
            }
    }

    @Override
    public void delete(Long id) {
        //删除产品服务
        super.delete(id);
        //删除产品服务明细
        productDetailMapper.delete(id);
    }
    /*批量删除*/
    @Override
    public void batchDelete(List<Product> list) {
        /*批量删除产品服务*/
        super.batchDelete(list);
        /*批量删除产品服务对应的明细表*/
        productDetailMapper.batchDeleteByProduct(list);
    }

    /**
     * 批量上架
     * @param products
     */
    @Override
    @Transactional
    public void onSale(List<Product> products) {

        productMapper.onSale(products);
    }
    /**
     * 批量下架
     * @param products
     */
    @Override
    @Transactional
    public void offSale(List<Product> products) {
        productMapper.offSale(products);
    }

    @Override
    public Product findProductById(Long id) {
        return productMapper.findProductById(id);
    }
}
